package com.ruoyi.common.websocket;

import com.ruoyi.common.core.domain.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.Token;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@ServerEndpoint("/websocket/{token}")
public class WebSocket {
    @Autowired
    TokenService tokenService;

    //存储session集合
    private static  ConcurrentHashMap<Long,Session> sessionMap=new ConcurrentHashMap<>();
    //存储map集合
    private static  ConcurrentHashMap<Long, LoginUser> userMap=new ConcurrentHashMap<>();



    @OnOpen
    public void onOpen(Session session, @PathParam(value = "token") String token){

        System.out.println("【webSocket连接成功】，此人token为：" + token);
        //判断token是否失效或者解析错误
        Token tokens = tokenService.verifyToken(token);
        if (tokens == null  ) {
          log.error("token失效或无法解析");
        }

        
    }

    @OnClose
    public void onClose(Session session){
        System.out.println("【webSocket退出成功】");
    }

    @OnError
    public void onError(Session session, Throwable throwable){

        System.out.println("error:");
        throwable.printStackTrace();
    }

    @OnMessage
    public void onMessage(Session session, String message){
        System.out.println("【webSocket接收成功】内容为：" + message);
    }



}
